package com.shangxia.lucky.lucky.job.write;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.shangxia.lucky.lucky.ConfigText;
import com.shangxia.lucky.lucky.common.FactoryHead;
import com.shangxia.lucky.lucky.writeHandler.MyMatchExcelWriteHandler;
import com.shangxia.lucky.lucky.writeHandler.MyTrueWriteHandler;
import com.shangxia.lucky.lucky.writeHandler.MyTypeThreadWriteHandler;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;

import java.util.*;

/**
 * @ProjectName: monomer
 * @ClassName: OverWriterThread
 * @Time: 2021/9/4
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
@Data
@NoArgsConstructor
public class MatchWriterThread implements Runnable, ExportThreadJobOver {


    public volatile Boolean MATCH_WRITE_OVER_FLAG;
    public volatile static Boolean WRITER_OVER_LOG = false;
    public final static LinkedList<List<List<String>>> ageQueue = new LinkedList<>();
    public final static LinkedList<List<List<String>>> typeQueue = new LinkedList<>();
    public static List<MyMatchExcelWriteHandler> WRITE_HANDLERS = new ArrayList<>();

    public ConfigText configText;

    public MatchWriterThread(ConfigText configText) {
        this.configText = configText;
        MATCH_WRITE_OVER_FLAG = !configText.getBananaOverExportOpen();
    }


    @Override
    public void run() {
        WriteSheet ageSheet = EasyExcel.writerSheet("统计")
                .registerWriteHandler(new MyTrueWriteHandler()).head(FactoryHead.ageHead(configText.getBananaExportColumn())).build();
        WriteSheet typeSheet = EasyExcel.writerSheet("种类")
                .registerWriteHandler(new MyTypeThreadWriteHandler()).head(FactoryHead.typeHead(configText.getBananaExportColumn())).build();
        String file = configText.getBananaAbsoluteResultPath() + DateFormatUtils.format(new Date(), "yyyy-MM-dd_H-mm-ss") + ".xlsx";
        ExcelWriter excelWriter = EasyExcel.write(file).build();
        log.info("结果输出 开始");
        int count = 0;
        while (!over() || !ageQueue.isEmpty() || !typeQueue.isEmpty()) {
            List<List<String>> ageLists = ageQueue.poll();
            if (null != ageLists) {
                if (count++ % 1000 == 0) {
                    log.info("输出结果第" + count + "行");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                excelWriter.write(ageLists, ageSheet);
            }
            List<List<String>> typeLists = typeQueue.poll();
            if (null != typeLists) {
                excelWriter.write(typeLists, typeSheet);
            }
        }
        excelWriter.finish();
        WRITER_OVER_LOG = true;
        log.info("----------结果输出结束----------------");
    }

    public void stop() {
        setMATCH_WRITE_OVER_FLAG(Boolean.TRUE);
    }

    @Override
    public Boolean over() {
        return MATCH_WRITE_OVER_FLAG;
    }
}
